import { defineEventHandler, getQuery } from 'h3';

// 模拟资源数据
const mockResources = [
  {
    id: '1',
    name: '酒店管理系统',
    type: 'service',
    category: 'hotel',
    version: '1.0.0',
    status: 'active',
    description: '完整的酒店管理解决方案',
    tags: ['酒店', '管理', 'PMS'],
    attributes: {
      capacity: '1000间房',
      features: ['预订', '入住', '退房', '财务'],
      integration: ['微信', '支付宝', '携程'],
    },
    capabilities: ['预订管理', '房态管理', '财务管理'],
    constraints: {
      maxRooms: 1000,
      maxUsers: 50,
      regions: ['CN'],
    },
    pricing: {
      customerPricing: {
        currency: 'CNY',
        basePrice: 999,
        unit: 'month',
        features: ['基础功能', '技术支持'],
      },
      platformPricing: {
        currency: 'CNY',
        cost: 299,
        margin: 700,
        revenueShare: 0.3,
      },
    },
    lifecycle: {
      status: 'active',
      createdAt: '2024-01-01T00:00:00Z',
      updatedAt: '2024-01-15T10:30:00Z',
      publishedAt: '2024-01-10T09:00:00Z',
    },
    metadata: {
      author: 'HotelTech',
      license: 'commercial',
      documentation: 'https://docs.hoteltech.com',
      support: 'support@hoteltech.com',
    },
  },
  {
    id: '2',
    name: '宠物店管理系统',
    type: 'service',
    category: 'pet',
    version: '0.9.0',
    status: 'draft',
    description: '宠物店业务管理平台',
    tags: ['宠物', '零售', '管理'],
    attributes: {
      capacity: '500宠物',
      features: ['宠物档案', '服务预约', '商品管理'],
      integration: ['微信', '支付宝'],
    },
    capabilities: ['宠物档案管理', '服务预约', '商品管理'],
    constraints: {
      maxPets: 500,
      maxUsers: 20,
      regions: ['CN'],
    },
    pricing: {
      customerPricing: {
        currency: 'CNY',
        basePrice: 599,
        unit: 'month',
        features: ['基础功能'],
      },
      platformPricing: {
        currency: 'CNY',
        cost: 199,
        margin: 400,
        revenueShare: 0.25,
      },
    },
    lifecycle: {
      status: 'draft',
      createdAt: '2024-01-12T14:30:00Z',
      updatedAt: '2024-01-15T16:20:00Z',
      publishedAt: null,
    },
    metadata: {
      author: 'PetTech',
      license: 'commercial',
      documentation: 'https://docs.pettech.com',
      support: 'support@pettech.com',
    },
  },
  {
    id: '3',
    name: '教育管理系统',
    type: 'service',
    category: 'education',
    version: '2.1.0',
    status: 'active',
    description: '教育机构综合管理平台',
    tags: ['教育', '管理', '在线学习'],
    attributes: {
      capacity: '10000学生',
      features: ['学生管理', '课程管理', '成绩管理'],
      integration: ['微信', '钉钉', '腾讯会议'],
    },
    capabilities: ['学生管理', '课程管理', '成绩管理', '在线学习'],
    constraints: {
      maxStudents: 10000,
      maxTeachers: 200,
      regions: ['CN'],
    },
    pricing: {
      customerPricing: {
        currency: 'CNY',
        basePrice: 1999,
        unit: 'month',
        features: ['完整功能', '技术支持', '培训'],
      },
      platformPricing: {
        currency: 'CNY',
        cost: 599,
        margin: 1400,
        revenueShare: 0.4,
      },
    },
    lifecycle: {
      status: 'active',
      createdAt: '2023-12-01T00:00:00Z',
      updatedAt: '2024-01-10T11:15:00Z',
      publishedAt: '2023-12-15T09:00:00Z',
    },
    metadata: {
      author: 'EduTech',
      license: 'commercial',
      documentation: 'https://docs.edutech.com',
      support: 'support@edutech.com',
    },
  },
  {
    id: '4',
    name: '零售POS系统',
    type: 'service',
    category: 'retail',
    version: '1.5.0',
    status: 'active',
    description: '零售店铺销售管理系统',
    tags: ['零售', 'POS', '销售'],
    attributes: {
      capacity: '1000商品',
      features: ['商品管理', '销售记录', '库存管理'],
      integration: ['微信支付', '支付宝', '银联'],
    },
    capabilities: ['商品管理', '销售记录', '库存管理', '会员管理'],
    constraints: {
      maxProducts: 1000,
      maxStores: 10,
      regions: ['CN'],
    },
    pricing: {
      customerPricing: {
        currency: 'CNY',
        basePrice: 799,
        unit: 'month',
        features: ['基础功能', '技术支持'],
      },
      platformPricing: {
        currency: 'CNY',
        cost: 299,
        margin: 500,
        revenueShare: 0.2,
      },
    },
    lifecycle: {
      status: 'active',
      createdAt: '2023-11-01T00:00:00Z',
      updatedAt: '2024-01-08T14:45:00Z',
      publishedAt: '2023-11-20T10:00:00Z',
    },
    metadata: {
      author: 'RetailTech',
      license: 'commercial',
      documentation: 'https://docs.retailtech.com',
      support: 'support@retailtech.com',
    },
  },
];

export default defineEventHandler(async (event) => {
  const query = getQuery(event);
  const { page = 1, pageSize = 10, type, category, status, keyword } = query;

  // 过滤数据
  let filteredData = [...mockResources];

  if (type) {
    filteredData = filteredData.filter((resource) => resource.type === type);
  }

  if (category) {
    filteredData = filteredData.filter(
      (resource) => resource.category === category,
    );
  }

  if (status) {
    filteredData = filteredData.filter(
      (resource) => resource.status === status,
    );
  }

  if (keyword) {
    const searchKeyword = keyword.toString().toLowerCase();
    filteredData = filteredData.filter(
      (resource) =>
        resource.name.toLowerCase().includes(searchKeyword) ||
        resource.description.toLowerCase().includes(searchKeyword) ||
        resource.tags.some((tag) => tag.toLowerCase().includes(searchKeyword)),
    );
  }

  // 分页
  const pageNum = parseInt(page.toString());
  const size = parseInt(pageSize.toString());
  const start = (pageNum - 1) * size;
  const end = start + size;
  const paginatedData = filteredData.slice(start, end);

  return {
    success: true,
    data: {
      list: paginatedData,
      pagination: {
        current: pageNum,
        pageSize: size,
        total: filteredData.length,
        totalPages: Math.ceil(filteredData.length / size),
      },
    },
    message: '查询成功',
  };
});
